<template>
  <el-container>
    <el-main v-loading="loading">
      <el-table :data="chagencyinList">
        <el-table-column prop="id" label="中医机构ID" width="200"/>
        <el-table-column prop="name" label="中医机构名称" width="280"/>
        <el-table-column prop="contactPerson" label="联系人" width="230"/>
        <el-table-column prop="phone" label="联系电话" width="300"/>
        <el-table-column prop="count" label="分院数量"/>
        <el-table-column label="操作" width="320">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="branchClick(scope.row)" type="text" size="small">分院管理</el-button>
            <el-button
              @click="stopClick(scope.row)"
              type="text"
              size="small"
            >{{scope.row.deleteFlag ? '上线': '下线'}}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <span>{{dialogContent}}</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="sure">确 定</el-button>
        </span>
      </el-dialog>
    </el-main>
  </el-container>
</template>
<script>
const tips = [
  "确认上线该机构信息?",
  "确认下线该机构信息?",
  "确认删除该机构信息?"
];
export default {
  name: "medicineTable",
  props: ["data", "loading"],
  data() {
    return {
      chagencyinList: [],
      dialogContent: "",
      dialogVisible: false,
      id: ""
    };
  },
  methods: {
    handleClick(params) {
      this.id = params.id;
      this.$router.push(`addOrg?type=edit&code=1&id=${this.id}`);
    },
    branchClick(params) {
      this.id = params.id;
      this.$router.push(`branch?code=1&id=${this.id}`);
    },
    stopClick(params) {
      this.id = params.id;
      this.dialogVisible = true;
      if (params.deleteFlag) return (this.dialogContent = tips[1]);
      this.dialogContent = tips[0];
    },
    sure() {
      let user = this.lodash.filter(this.chagencyinList, item => {
        return item.id === this.id;
      });
      let { deleteFlag, id, name, contactPerson, phone } = user[0];
      deleteFlag = !deleteFlag;
      let params = { deleteFlag, id, name, contactPerson, phone };
      this.api
        .put(`/chinaMed`, params, "form")
        .then(() => {
          this.$emit("search");
        })
        .catch(err => {
          console.log(err);
          this.$message(err.data.message);
        });
      this.dialogVisible = false;
    }
  },
  watch: {
    data(newValue) {
      this.chagencyinList = newValue;
    }
  }
};
</script>
<style lang="less" scoped>
</style>